package net.worthtech.callcasher;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by RigerWu on 2018/9/18.
 */
public class HaiNanTestActivity extends AppCompatActivity {
    private android.widget.EditText money;
    private android.widget.EditText order;
    private android.widget.Button hu;
    private android.widget.Button bsp;
    private android.widget.Button baoxian;
    private android.widget.Button shgq;
    private android.widget.Button yunjipiao;
    private android.widget.Button feihu;
    private android.widget.Button refund;
    private android.widget.Button check;
    private String mMoney;
    private String mOrder;
    private String mTrace;

    public static void launch(Context context) {
        Intent intent = new Intent(context, HaiNanTestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hntest);
        this.check = (Button) findViewById(R.id.check);
        this.refund = (Button) findViewById(R.id.refund);
        this.feihu = (Button) findViewById(R.id.feihu);
        this.yunjipiao = (Button) findViewById(R.id.yunjipiao);
        this.shgq = (Button) findViewById(R.id.shgq);
        this.baoxian = (Button) findViewById(R.id.baoxian);
        this.bsp = (Button) findViewById(R.id.bsp);
        this.hu = (Button) findViewById(R.id.hu);
        this.order = (EditText) findViewById(R.id.order);
        this.money = (EditText) findViewById(R.id.money);

        hu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMoney = money.getText().toString();
                mOrder = order.getText().toString();
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("net.worthtech.worthcasherhna",
                        "net.worthtech.worthcasherhna.activity.PayService"));
                intent.putExtra("option", "consume");
                
                intent.putExtra("amount", mMoney);
                intent.putExtra("clearIdentify", "FZ21073006HU");
                intent.putExtra("orderNo", mOrder);
                startActivityForResult(intent, 0);
            }
        });

        bsp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMoney = money.getText().toString();
                mOrder = order.getText().toString();
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("net.worthtech.worthcasherhna",
                        "net.worthtech.worthcasherhna.activity.PayService"));
                intent.putExtra("option", "consume");
                
                intent.putExtra("amount", mMoney);
                intent.putExtra("clearIdentify", "FZ21073011BSP");
                intent.putExtra("orderNo", mOrder);
                startActivityForResult(intent, 0);
            }
        });

        baoxian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMoney = money.getText().toString();
                mOrder = order.getText().toString();
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("net.worthtech.worthcasherhna",
                        "net.worthtech.worthcasherhna.activity.PayService"));
                intent.putExtra("option", "consume");
                
                intent.putExtra("amount", mMoney);
                intent.putExtra("clearIdentify", "FZ21073012BX");
                intent.putExtra("orderNo", mOrder);
                startActivityForResult(intent, 0);
            }
        });

        shgq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMoney = money.getText().toString();
                mOrder = order.getText().toString();
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("net.worthtech.worthcasherhna",
                        "net.worthtech.worthcasherhna.activity.PayService"));
                intent.putExtra("option", "consume");
                
                intent.putExtra("amount", mMoney);
                intent.putExtra("clearIdentify", "FZ21073013SHGQ");
                intent.putExtra("orderNo", mOrder);
                startActivityForResult(intent, 0);
            }
        });

        yunjipiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMoney = money.getText().toString();
                mOrder = order.getText().toString();
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("net.worthtech.worthcasherhna",
                        "net.worthtech.worthcasherhna.activity.PayService"));
                intent.putExtra("option", "consume");
                
                intent.putExtra("amount", mMoney);
                intent.putExtra("clearIdentify", "FZ21073011HSY");
                intent.putExtra("orderNo", mOrder);
                startActivityForResult(intent, 0);
            }
        });

        feihu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMoney = money.getText().toString();
                mOrder = order.getText().toString();
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("net.worthtech.worthcasherhna",
                        "net.worthtech.worthcasherhna.activity.PayService"));
                intent.putExtra("option", "consume");
                
                intent.putExtra("amount", mMoney);
                intent.putExtra("clearIdentify", "FZ21073011JTN");
                intent.putExtra("orderNo", mOrder);
                startActivityForResult(intent, 0);
            }
        });

        refund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("net.worthtech.worthcasherhna",
                        "net.worthtech.worthcasherhna.activity.PayService"));
                intent.putExtra("option", "refund");
                intent.putExtra("refundTrace", mTrace);
                startActivityForResult(intent, 0);
            }
        });

        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("net.worthtech.worthcasherhna",
                        "net.worthtech.worthcasherhna.activity.PayService"));
                intent.putExtra("option", "check");
                intent.putExtra("orderNo", order.getText().toString());
                startActivityForResult(intent, 2);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("wurui", "MainActivity.onActivityResult:" + requestCode + "#" + resultCode + data);

        if (requestCode == 1) {
            String mer_name = data.getStringExtra("mer_name");
            String mer_id = data.getStringExtra("mer_id");
            String ter_id = data.getStringExtra("ter_id");
            LogUtils.i("MainActivity.onActivityResult->:" + mer_name + mer_id + ter_id);
        }

        if (requestCode == 2) {
            if (data == null) {
                Toast.makeText(this, "未查询", Toast.LENGTH_SHORT).show();
                return;
            }
            String result = data.getStringExtra("result");
            mTrace = data.getStringExtra("trace");
            Toast.makeText(this, order.getText().toString() + ":" + result, Toast.LENGTH_SHORT).show();
            LogUtils.i("MainActivity.onActivityResult->:" + data.getStringExtra("printInfo"));
        }

        if (requestCode == 4) {
            String printInfo = data.getStringExtra("printInfo");

            LogUtils.i("MainActivity.onActivityResult->:" + printInfo);
        }

        if (requestCode == 5) {
            String result = data.getStringExtra("result");
            LogUtils.i("MainActivity.onActivityResult->:" + result);
            if (result.equals("00")) {
                Toast.makeText(this, "签到成功", Toast.LENGTH_SHORT).show();
            }
        }

        if (requestCode != 0) {
            return;
        }

        if (data != null) {
            LogUtils.i("MainActivity.onActivityResult->:" + data.getStringExtra("paytype"));
            LogUtils.i("MainActivity.onActivityResult->:" + data.getStringExtra("trace"));
            LogUtils.i("MainActivity.onActivityResult->:" + data.getStringExtra("referenceNo"));
            LogUtils.i("MainActivity.onActivityResult->:" + data.getStringExtra("amount"));
            LogUtils.i("MainActivity.onActivityResult->:" + data.getStringExtra("printInfo"));
            String result = data.getStringExtra("result");
            if (result.equals("00")) {
                mTrace = data.getStringExtra("trace");
                String referenceNo = data.getStringExtra("referenceNo");
                String cardNo = data.getStringExtra("cardNo");

                AlertDialog.Builder builder = new AlertDialog.Builder(HaiNanTestActivity.this);
                builder.setTitle("交易成功");
                builder.setMessage("交易金额为：" + mMoney +
                        "\n流水号：" + mTrace +
                        "\n参考号：" + referenceNo +
                        "\n卡号：" + cardNo);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });
                builder.show();
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(HaiNanTestActivity.this);
                builder.setTitle("交易失败");
                builder.setMessage("返回码为：" + result);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });
                builder.show();
            }
        } else {
            Toast.makeText(this, "未做支付操作", Toast.LENGTH_SHORT).show();
        }
    }
}
